<template>
  <a-input-group compact>
    <a-select v-model:value="key" class="w-108 my-select" @change="changeCondition">
      <a-select-option v-for="item in condition" :key="item.key" :value="item.key" :title="item.name"> {{ item.name }} </a-select-option>
    </a-select>
    <a-input v-model:value="inputValue" v-trim class="w-287 mx--1" allow-clear :placeholder="`请输入${name}`" @keyup.enter="search" />
    <a-button @click="search"><i class="iconfont icon-search-line"></i></a-button>
  </a-input-group>
</template>

<script lang="ts" setup>
  import { computed, ref } from 'vue'

  export interface Props {
    condition: {
      key: string
      name: string
    }[]
  }

  export type SearchFn = (obj: Record<string, string>) => void

  const props = withDefaults(defineProps<Props>(), {})
  const emits = defineEmits(['search'])

  const searchObj: Record<string, string> = {} //返回给组件外的值
  props.condition.forEach(it => (searchObj[it.key] = ''))
  console.log('searchObj', searchObj)
  console.log('props.condition', props.condition)

  const key = ref(props.condition[0].key) //条件key
  const name = computed(() => props.condition.find(it => it.key === key.value)?.name || '') //条件名称
  const inputValue = ref('')

  const changeCondition = () => {}
  const search = () => {
    for (const k in searchObj) {
      searchObj[k] = key.value === k ? inputValue.value : ''
    }
    emits('search', searchObj)
  }
</script>
